#include <modelManager.h>

namespace Model {

  ModelManager::ModelManager()
  {
    model *setting = new SystemSettingMessage();
    model *attack = new AttackerMessage();
    model *attackTarget = new AttackTargetMessage();
    AddModel(static_cast<std::string>("SystemSettingMessage"),setting);
    AddModel(static_cast<std::string>("AttackerMessage"),attack);
    AddModel(static_cast<std::string>("AttackTargetMessage"),attackTarget);


    Attacker *arpAttacker = new ArpAttacker(&models);
    attackers[ARP_ATTACK] = arpAttacker;
    Attacker *synAttacker = new SynAttacker(&models);
    attackers[SYN_ATTACK] = synAttacker;
    Attacker *smurfAttacker = new SmurfAttacker(&models);
    attackers[SMURF_ATTACK] = smurfAttacker;
  }

  ModelManager::~ModelManager()
  {

  }

  bool ModelManager::ReceiveBroadcast(Controller::Broadcast *broadcast)
  { 
    std::map<std::string,model *>::iterator Mit;
    for ( Mit = models.begin() ; Mit != models.end() ; Mit ++ )
      (Mit->second)->ReceiveBroadcast(broadcast);
   
    if(Controller::BROADCAST_TYPE_ATTACK_STAT 
       == broadcast->GetBroadcastType()){
      AttackType type = static_cast<SystemSettingMessage*>(models["SystemSettingMessage"])->GetCurrentAttack();
      if(true == broadcast->GetBoolData())
	attackers[type]->StartAttack();
      else
	attackers[type]->StopAttack();
      //std::map<AttackType,Attacker*>::iterator Ait;
      //      for( Ait = attackers.begin() ; Ait != attackers.end() ; Ait ++)
      //if(true == broadcast->GetBoolData())
      //  (Ait->second)->StartAttack();
      //else
      //  (Ait->second)->StopAttack();
    }else if(Controller::BROADCAST_TYPE_EXIT
	     == broadcast->GetBroadcastType()){
      std::map<AttackType,Attacker*>::iterator it;
      for(it = attackers.begin() ; it != attackers.end() ; it ++)
	it->second->StopAttack();
    }
    return true;
  }

  bool ModelManager::AddModel(std::string modelName,model *newModel)
  {
    models[modelName] = newModel;
    return true;
  }

  int ModelManager::ModelNum()
  {
    return models.size();
  }

  bool ModelManager::ReLoadConf()
  {
    std::map<std::string,model *>::iterator it;
    for ( it = models.begin() ; it != models.end() ; it ++ )
      it->second->ReLoadConf();
    return true;
  }

}
